package com.app.news;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewOutlineProvider;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import com.app.news.bean.News;
import com.app.news.util.GetJsonNewsUtil;
import com.app.news.util.SpSave;
import com.google.gson.Gson;
import com.squareup.picasso.Picasso;

import java.util.List;

public class TwoActivity extends AppCompatActivity {

    private ListView listView;
    private List<News.ResultBean.DataBean> dataall;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_two);
        initView();
        initData();
        initEvent();
    }
    private void initView(){
        listView = findViewById(R.id.new_listview);
    }
    private void initData(){
        String jsonNews=new GetJsonNewsUtil().getJson(TwoActivity.this,"News.json");
        Gson gson=new Gson();
        News news=gson.fromJson(jsonNews,News.class);
        dataall = news.getResult().getData();
        MyAdapater myAdapater=new MyAdapater(TwoActivity.this, dataall);
        listView.setAdapter(new MyAdapater(TwoActivity.this, dataall));
    }
    private void initEvent(){
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(TwoActivity.this,ShowActivity.class);
                intent.putExtra("image1",dataall.get(position).getThumbnail_pic_s());
                intent.putExtra("image2",dataall.get(position).getThumbnail_pic_s02());
                intent.putExtra("image3",dataall.get(position).getThumbnail_pic_s03());
                intent.putExtra("title",dataall.get(position).getTitle());
                intent.putExtra("getAuthor_name",dataall.get(position).getAuthor_name());
                intent.putExtra("date",dataall.get(position).getDate());
                intent.putExtra("url",dataall.get(position).getUrl());
                startActivity(intent);
            }
        });
    }
    class MyAdapater extends BaseAdapter{
        private Context context;
        private List<News.ResultBean.DataBean> dataBeanlist;
        public MyAdapater(Context context, List<News.ResultBean.DataBean> dataBeanlist) {
            this.context = context;
            this.dataBeanlist = dataBeanlist;
        }
        @Override
        public int getCount() {
            return dataBeanlist.size();
        }
        @Override
        public Object getItem(int position) {
            return dataBeanlist.get(position);
        }
        @Override
        public long getItemId(int position) {
            return position;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView= View.inflate(context,R.layout.listview_item_layout,null);
            ImageView imageView=convertView.findViewById(R.id.image_v);
            TextView textView1=convertView.findViewById(R.id.text1_tv);
            TextView textView2=convertView.findViewById(R.id.text2_tv);
            imageView.setImageURI(Uri.parse(dataBeanlist.get(position).getThumbnail_pic_s()));
            textView1.setText(dataBeanlist.get(position).getTitle()+"");
            textView2.setText(dataBeanlist.get(position).getAuthor_name()+"");
            Picasso.with(getApplicationContext()).load(dataBeanlist.get(position).getThumbnail_pic_s()).into(imageView);
            return convertView;
        }
    }
}
